<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加垃圾投放记录</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            color: #333;
        }
        .container {
            max-width: 1200px;
            margin: 20px auto;
            padding: 20px;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }
        h1 {
            text-align: center;
            color: #007BFF;
            margin-bottom: 20px;
        }
        form {
            display: flex;
            flex-direction: column;
            max-width: 600px;
            margin: 0 auto;
        }
        label {
            margin-bottom: 5px;
            font-weight: bold;
        }
        input, select {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-bottom: 15px;
        }
        button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        .error {
            color: red;
            text-align: center;
            margin-top: 10px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>添加垃圾投放记录</h1>
    </div>
    <form id="addWasteRecordForm">
        <label for="residentId">居民编号：</label>
        <input type="text" id="residentId" name="residentId" required>

        <label for="type">垃圾类型：</label>
        <select id="type" name="type" required>
            <option value="">请选择垃圾类型</option>
            <option value="可回收物">可回收物</option>
            <option value="厨余垃圾">厨余垃圾</option>
            <option value="有害垃圾">有害垃圾</option>
            <option value="其他垃圾">其他垃圾</option>
        </select>

        <label for="time">投放时间：</label>
        <input type="datetime-local" id="time" name="time" required>

        <label for="weight">重量 (kg)：</label>
        <input type="number" id="weight" name="weight" step="0.1" required>

        <label for="location">投放地点：</label>
        <input type="text" id="location" name="location" required>

        <label for="status">审核状态：</label>
        <select id="status" name="status" required>
            <option value="">请选择审核状态</option>
            <option value="已审核">已审核</option>
            <option value="待审核">待审核</option>
        </select>

        <button type="submit">提交</button>
        <div class="error" id="error-message"></div>
    </form>
</div>

<script>
    document.getElementById('addWasteRecordForm').addEventListener('submit', function(event) {
        event.preventDefault();

        const residentId = document.getElementById('residentId').value;
        const type = document.getElementById('type').value;
        const time = document.getElementById('time').value;
        const weight = document.getElementById('weight').value;
        const location = document.getElementById('location').value;
        const status = document.getElementById('status').value;

        if (!residentId || !type || !time || !weight || !location || !status) {
            document.getElementById('error-message').innerText = '请填写所有必填项！';
            return;
        }

        const data = {
            residentId: Number(residentId),
            type,
            time,
            weight: Number(weight),
            location,
            status
        };

        fetch('http://localhost:8081/waste/addwasteRecord', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('提交失败');
                }
                return response.json();
            })
            .then(data => {
                alert('提交成功！');
                document.getElementById('addWasteRecordForm').reset();
            })
            .catch(error => {
                console.error('错误:', error);
                document.getElementById('error-message').innerText = '提交失败，请稍后再试！';
            });
    });
</script>
</body>
</html>